package cn.controller;

import cn.entities.MenuInfo;
import cn.entities.RoleInfo;
import cn.entities.UserInfo;
import cn.entities.UserRole;
import cn.service.MenuService;
import cn.service.RoleService;
import cn.service.UserService;
import cn.vo.ResultVo;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户模块控制器
 * */
@Controller
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private MenuService menuService;
    @Autowired
    private RoleService roleService;

    /**
     * 跳转登录页
    * */
    @GetMapping("/login")
    public String loginUI(){
        return "login";
    }
    /**
     * 跳转首页
    * */
    @GetMapping("/index")
    public String indexUI(){
        return "index";
    }
    @GetMapping("/")
    public String indexUI2(){
        return "redirect:/index";
    }

    @PostMapping("/login")
    @ResponseBody
    public ResultVo<Object> login(UserInfo param, HttpSession session){
        System.out.println("User:"+param);

        UserInfo user = userService.login(param.getUsername(), param.getPsw());
        if(user!=null){
            session.setAttribute("cnUser",user);
            return new ResultVo<>(20000,"登录成功",null);
        }
        return new ResultVo<>(20001,"用户名或密码错误",null);
    }

    @GetMapping("/menu")
    @ResponseBody
    public ResultVo<List<MenuInfo>> getAllMenu(@RequestParam(name = "id",required = false) String id){
        Integer pid = 0;
        if(id==null){pid = 0;}else {
            pid = Integer.parseInt(id.split("\\|")[0]);
        }
        List<MenuInfo> alluserRole = menuService.getMenu(pid);
        return new ResultVo<>(2000,"查询成功",alluserRole);
    }

    @GetMapping("role/view")
    public String toRole(){
        return "role";
    }
    @GetMapping("/role")
    @ResponseBody
    public ResultVo<Map<String ,Object>> getRole(@RequestParam("page")Integer pageNumber,
                                            @RequestParam("rows")Integer pageSize){
        Page<List<RoleInfo>> pageInfo = roleService.getRole(pageNumber,pageSize);
        Map<String ,Object> data = new HashMap<>();
        data.put("total", pageInfo.getTotal());
        data.put("rows", pageInfo.getResult());
        return new ResultVo<>(2000,"查询成功",data);
    }

    @PostMapping("/role")
    @ResponseBody
    public ResultVo<Object> addRole(RoleInfo roleInfo){
        roleService.addRole(roleInfo);
        System.out.println("新增");
        return new ResultVo<>(20000,"新增成功",null);
    }
    @PutMapping("/role")
    @ResponseBody
    public ResultVo<Object> updateRole(RoleInfo roleInfo){
        roleService.updateRole(roleInfo);
        System.out.println("修改");
        return new ResultVo<>(20000,"修改成功",null);
    }

    @GetMapping("/role/{id}")
    @ResponseBody
    public RoleInfo getRoleById(@PathVariable("id")Integer id){
        RoleInfo roleInfo = roleService.getRoleById(id);
        return roleInfo;
    }

    @DeleteMapping("/role")
    @ResponseBody
    public ResultVo<Object> delRoleById(@RequestParam("ids")String ids){
        roleService.deletRole(ids);
        return new ResultVo<>(20000,"删除成功",null);
    }

}
